﻿using System.Collections.Generic;
using System.Linq;
using Model.Entities;
using Model.Repositories;

namespace Presenter.Users
{
    public class UserGroupsPresenter
    {
        private IUserGroupsView _view;

        public UserGroupsPresenter(IUserGroupsView view, int id)
        {
            _view = view;

            _view.GetGroups(GetGroups(id));
            _view.GetOwnedGroups(GetOwnedGroups(id));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        void ViewEntityCreated(string email, string password, string title, string type)
        {
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null && RepositoryManager.Instance.GroupRepository.GetByTitle(title) == null)
            {
                obj.OwnedGroups.Add(new Group { Title = title, Type = type, Owner = obj });
                RepositoryManager.Instance.UserRepository.Update(obj);
            }
        }

        void ViewEntityDeleted(string email, string password, int id)
        {
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                obj.Interests.Remove(obj.Interests.FirstOrDefault(o => o.Id == id));
                RepositoryManager.Instance.UserRepository.Update(obj);
            }
        }

        public UserGroupsPresenter(IUserGroupsView view, string email, string password)
        {
            _view = view;

            _view.GetGroups(GetGroups(email, password));
            _view.GetOwnedGroups(GetOwnedGroups(email, password));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        public void Reload(IUserGroupsView view, int id)
        {
            _view = view;

            _view.GetGroups(GetGroups(id));
            _view.GetOwnedGroups(GetOwnedGroups(id));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        public void Reload(IUserGroupsView view, string email, string password)
        {
            _view = view;

            _view.GetGroups(GetGroups(email, password));
            _view.GetOwnedGroups(GetOwnedGroups(email, password));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        Dictionary<int, string> GetGroups(int id)
        {
            var result = new Dictionary<int, string>();

            var obj = RepositoryManager.Instance.UserRepository.Get(id);
            if (obj != null)
            {
                foreach (var g in obj.Groups)
                {
                    result.Add(g.Id, g.Title);
                }
            }

            return result;
        }

        Dictionary<int, string> GetGroups(string email, string password)
        {
            var result = new Dictionary<int, string>();

            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                foreach (var g in obj.Groups)
                {
                    result.Add(g.Id, g.Title);
                }
            }

            return result;
        }

        Dictionary<int, string> GetOwnedGroups(int id)
        {
            var result = new Dictionary<int, string>();

            var obj = RepositoryManager.Instance.UserRepository.Get(id);
            if (obj != null)
            {
                foreach (var g in obj.OwnedGroups)
                {
                    result.Add(g.Id, g.Title);
                }
            }

            return result;
        }

        Dictionary<int, string> GetOwnedGroups(string email, string password)
        {
            var result = new Dictionary<int, string>();

            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                foreach (var g in obj.OwnedGroups)
                {
                    result.Add(g.Id, g.Title);
                }
            }

            return result;
        }
    }
}

